package com.cssy.xxkj.basic.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.constants.Constants;
import com.cssy.xxkj.basic.entity.BasicCompanyTestDO;
import com.cssy.xxkj.basic.service.IBasicCompanyTestService;
import com.cssy.xxkj.basic.service.IBasicContactRecordService;
import com.cssy.xxkj.basic.service.IBasicCustomerContactService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
/**
 * 客户管理
 * @author houfei
 *
 */
@Controller
@RequestMapping("/basic/customer")
public class CustomerController extends BaseController<BasicCompanyTestDO> {
	
	private static final String MANAGER = "/xxkj/basic/customer/CustomerManager";// 列表页面
	private static final String EIDT = "/xxkj/basic/customer/CustomerOperation";// 添加、修改、显示页面

	@Resource(name = "basicCompanyTestService")
	private IBasicCompanyTestService basicCompanyTestService;

	@Resource(name="basicCustomerContactService")
	private IBasicCustomerContactService basicCustomerContactService;
	
	@Resource(name="basicContactRecordService")
	private IBasicContactRecordService basicContactRecordService;

	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty(null);
		scs.addCondition("zd032Type", Constants.COMPANY_TYPE_CUST, SearchCondition.OP_EQUAL);
		SearchResult result= basicCompanyTestService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);

	}

	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
					throws Exception {

		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			model.addAttribute("flag", "ADD");
		} else {
			BasicCompanyTestDO bcd =basicCompanyTestService.findListById(id);
			model.addAttribute("customer", bcd);
			model.addAttribute("flag", "EDIT");
		}
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicCompanyTestDO curAP, BindingResult result, Model model) {
		BodyMsg bodyMap =basicCustomerContactService.saveOrUpdate(request, curAP);
		return bodyMap.modelMap;

	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String isDelete = request.getParameter("isDelete");//标识是否是假删除
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(isDelete==null||"0".equals(isDelete)){
					basicCompanyTestService.delete(field, SessionUtil.getUser(request));
				}else{
					basicCompanyTestService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	/**
	 * 客户联系人列表
	 */
	@RequestMapping(value="/contactList")
	public void queryContactList(HttpServletRequest request, HttpServletResponse response){
		String custId=request.getParameter("zd001CustId");
		if(StringUtils.isNotBlank(custId)){
			JqGridHandler hander=new JqGridHandler(request);
			SearchConditions scs = new SearchConditions();
			hander.getWheres(scs);
			scs.setRemovedProperty("removed");
			scs.addCondition("zd001CustId",custId, SearchCondition.OP_EQUAL);
			SearchResult result= basicCustomerContactService.findListByExample(scs);
			OutJsonGrid out=new OutJsonGrid(result);
			out.printJSon(response);
		}
	}
	/**
	 * 联系记录列表
	 */
	@RequestMapping(value="/recordList")
	public void queryRecordList(HttpServletRequest request, HttpServletResponse response){
		String custId=request.getParameter("zd001CustId");
		if(StringUtils.isNotBlank(custId)){
			JqGridHandler hander=new JqGridHandler(request);
			SearchConditions scs = new SearchConditions();
			hander.getWheres(scs);
			scs.setRemovedProperty(null);
			scs.addCondition("zd001CustId",custId, SearchCondition.OP_EQUAL);
			SearchResult result= basicContactRecordService.findListByExample(scs);
			OutJsonGrid out=new OutJsonGrid(result);
			out.printJSon(response);
		}
	}
}
